package com.weatherquery.servlet;

import com.weatherquery.service.WeatherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Natural-Pride
 * @Date 2023/12/18 17:35
 * @注释 使用 Java Servlet规范中的@WebServlet注解,将该类作为一个 Servlet类进行配置,
 *       该 Servlet用于处理"/WeatherDeleteServlet"的 HTTP请求
 */
@WebServlet("/WeatherDeleteServlet")
public class WeatherDeleteServlet extends HttpServlet{

    //重写 HttpServlet的 doGet方法,处理 GET请求
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //调用 doPost方法处理请求
        doPost(request, response);
    }

    //重写 HttpServlet的 doPost方法,处理 POST请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //从请求中获取 cityId参数的值
        String cityId = request.getParameter("cityId");
        //创建一个 WeatherService对象,用于操作天气数据
        WeatherService weatherService = new WeatherService();
        //调用 WeatherService的 deleteWeather方法,删除指定的天气数据
        boolean delete = weatherService.deleteWeather(cityId);
        //根据删除操作的结果,设置提示信息
        String tips = delete ?"<label style='color:green'>删除成功</label>":"<label style='color:red'>删除失败</label>";
        //将提示信息作为属性,放入请求对象中
        request.setAttribute("tips", tips);
        //通过请求Dispatcher,转发请求到"prompt.jsp"页面
        request.getRequestDispatcher("prompt.jsp").forward(request, response);
    }
}

